package utl.mofen.delphi.cds.convert;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;

/*
<?xml version="1.0" standalone="yes"?><DATAPACKET Version="2.0">
<METADATA>
<FIELDS>
<FIELD attrname="fKey" fieldtype="string" SUBTYPE="Guid" WIDTH="38"/>
<FIELD attrname="FInt" fieldtype="i4"/>
<FIELD attrname="FSmallint" fieldtype="i2"/>
<FIELD attrname="FTinyInt" fieldtype="i2"/>
<FIELD attrname="Fbigint" fieldtype="i8"/>
<FIELD attrname="FNumeric18_0" fieldtype="fixed" WIDTH="18"/>
<FIELD attrname="FNumeric18_4" fieldtype="fixed" DECIMALS="4" WIDTH="18"/>
<FIELD attrname="FNumeric18_10" fieldtype="fixed" DECIMALS="10" WIDTH="18"/>
<FIELD attrname="Fdecimal18_101" fieldtype="fixed" DECIMALS="4" WIDTH="18"/>
<FIELD attrname="Ffloat" fieldtype="r8"/>
<FIELD attrname="FMoney" fieldtype="fixed" DECIMALS="4" WIDTH="19"/>
<FIELD attrname="FText" fieldtype="bin.hex" SUBTYPE="Text"/>
<FIELD attrname="FVarchar" fieldtype="string" WIDTH="50"/>
<FIELD attrname="FNVarchar" fieldtype="string" SUBTYPE="string.uni" WIDTH="1000"/>
<FIELD attrname="FImage" fieldtype="bin.hex" SUBTYPE="Binary"/>
<FIELD attrname="FBinary" fieldtype="bin.hex" WIDTH="50"/>
<FIELD attrname="FChar" fieldtype="string" SUBTYPE="FixedChar" WIDTH="10"/>
<FIELD attrname="FNChar" fieldtype="string" WIDTH="20"/>
<FIELD attrname="FDatetime" fieldtype="dateTime"/>
<FIELD attrname="FDate" fieldtype="string" SUBTYPE="string.uni" WIDTH="20"/>
<FIELD attrname="FTime" fieldtype="string" SUBTYPE="string.uni" WIDTH="32"/>
<FIELD attrname="Ftimestamp" fieldtype="bin.hex" WIDTH="8"/>
</FIELDS>
<PARAMS/>
</METADATA>
<ROWDATA>
</ROWDATA>
 */


public class Column {

	public static String FieldInfo(ResultSetMetaData rsmd, int i) throws Exception
	{
		String ret = "";		

		int sqltype = rsmd.getColumnType(i);
		
		String tyName = rsmd.getColumnTypeName(i);
		
		String cName = rsmd.getColumnName(i);
		if(cName.equals(""))
		{
			cName = "column" + i;
		}
		
		//输出字段的信息类型
//		System.out.print("" + cName + "\n");
//		System.out.print("type:" + rsmd.getColumnType(i)+ "\n");
//		System.out.print("typeName:" + rsmd.getColumnTypeName(i)+ "\n");
//		System.out.print("Precision:" + rsmd.getPrecision(i)+ "\n");
//		System.out.print("Scale:" + rsmd.getScale(i)+ "\n");
//		System.out.print("===============\n");
		
		
		switch (sqltype) {
		case Types.CHAR:
			if (tyName.equals("char"))
			{
				ret = "<FIELD attrname=\""+ cName
				           +"\" fieldtype=\"string\" SUBTYPE=\"FixedChar\"" +
				           " WIDTH=\""+ String.valueOf(rsmd.getPrecision(i)) +"\"/>";
			}else
			{
				ret = "<FIELD attrname=\""+ cName
			           +"\" fieldtype=\"string\" SUBTYPE=\"Guid\" WIDTH=\"38\"/>";
			}			
			break;
		case Types.NCHAR:
			ret = "<FIELD attrname=\""+ cName
				           +"\" fieldtype=\"string\"" +
				           " WIDTH=\""+ String.valueOf(rsmd.getPrecision(i)*2) +"\"/>";

			break;
		case Types.LONGVARCHAR:  //-1 text
			ret = "<FIELD attrname=\""+ cName
			           +"\" fieldtype=\"bin.hex\" SUBTYPE=\"Text\"/>";	
				
				//<FIELD attrname="FText" fieldtype="bin.hex" SUBTYPE="Text"/>
			break;
		case Types.NVARCHAR:
			ret = "<FIELD attrname=\""+ cName
	           +"\" fieldtype=\"string\" SUBTYPE=\"string.uni\"" +
	           " WIDTH=\""+ String.valueOf(rsmd.getPrecision(i)*2) +"\"/>";
			//<FIELD attrname="FNVarchar" fieldtype="string.uni" WIDTH="1000"/>
			break;
		case -7:
			ret = "<FIELD attrname=\""+ cName
			           +"\" fieldtype=\"boolean\"\"/>";
			break;

		case Types.BINARY:   //timestamp, Binary
			ret = "<FIELD attrname=\""+ cName + "\""
			        + " fieldtype=\"bin.hex\""
					+ " WIDTH=\""+ String.valueOf(rsmd.getPrecision(i)) +"\""
					+ "/>";					
			break;	
		case Types.BLOB:   //, image(sql2000)?
		case Types.LONGVARBINARY:   //image
			ret = "<FIELD attrname=\""+ cName + "\""
			        + " fieldtype=\"bin.hex\""
					+ " SUBTYPE=\"Binary\""
					+ "/>";					
			break;	
		case Types.NUMERIC :   //numeric
		case Types.DECIMAL :   //decimal,money
			int scale =rsmd.getScale(i); 
			if (scale==0)
			{
				ret = "<FIELD attrname=\""+ cName + "\""
				        + " fieldtype=\"fixed\""
						+ " WIDTH=\""+ String.valueOf(rsmd.getPrecision(i))+"\""
						+ "/>";			
				
			} else
			{
				ret = "<FIELD attrname=\""+ cName + "\""
				        + " fieldtype=\"fixed\""
				        + " DECIMALS=\"" + String.valueOf(scale) + "\""
						+ " WIDTH=\""+ String.valueOf(rsmd.getPrecision(i))+"\""
						+ "/>";
			}
			break;	
		case Types.TINYINT:   //
			ret = "<FIELD attrname=\""+ cName
			           +"\" fieldtype=\"i2\"/>";
			break;	
		case Types.SMALLINT:  
			ret = "<FIELD attrname=\""+ cName
			           +"\" fieldtype=\"i2\"/>";
			break;	
		case Types.INTEGER:   //
			ret = "<FIELD attrname=\""+ cName
			           +"\" fieldtype=\"i4\"/>";
			break;	
		case Types.BIGINT:   
			ret = "<FIELD attrname=\""+ cName
			           +"\" fieldtype=\"i8\"/>";
			break;	
		case Types.DOUBLE:   
			ret = "<FIELD attrname=\""+ cName
			           +"\" fieldtype=\"r8\"/>";   //float
			break;			
		case 12:
			ret = "<FIELD attrname=\""+ cName
	           +"\" fieldtype=\"string\" WIDTH=\""+ String.valueOf(rsmd.getPrecision(i)) +"\"/>";
			break;
		case 93:
			ret = "<FIELD attrname=\""+ cName
	           +"\" fieldtype=\"dateTime\"/>";
			break;
			
		default:
			throw new Exception("未知的字段" +cName + "类型:" + sqltype);
			//ret = "<Unkown FIELD attrname=\""+ cName
	        //   +"\" fieldtype=\""+String.valueOf(sqltype) +"\" WIDTH=\""+ String.valueOf(rsmd.getPrecision(i)) +"\"/>";
			//break;
		}
		return ret;
		
	}
}
